/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.eventrouting.elvin;

import java.io.IOException;
import java.net.ConnectException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.avis.client.*;
import org.avis.common.InvalidURIException;
import uvexposureapplication.eventrouting.Consumer;

/**
 *
 * @author matthieukluj
 */
public class ElvinConsumer implements Consumer {

    private Elvin elvin;
    private NotificationListener ntf;
    private Subscription sub;

    public ElvinConsumer(String url, NotificationListener ntf) {
        try {
            this.elvin = new Elvin(url);
            this.ntf = ntf;
        } catch (InvalidURIException ex) {
            Logger.getLogger(ElvinConsumer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(ElvinConsumer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ConnectException ex) {
            Logger.getLogger(ElvinConsumer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ElvinConsumer.class.getName()).log(Level.SEVERE, null, ex);
        }
        //System.out.println("Consumer Connected");
    }

    public void subscribe(String parameters) {
        //System.out.println(parameters);
        try {
            this.sub = elvin.subscribe(parameters);
            this.sub.addListener(this.ntf);
        } catch (InvalidSubscriptionException ex) {
            Logger.getLogger(ElvinConsumer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ElvinConsumer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void closeConnection() {
        try {
            this.sub.removeListener(this.ntf);
            this.sub.remove();
            this.elvin.close();
        } catch (IOException ex) {
            Logger.getLogger(ElvinConsumer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
